package com.project.controller;

import com.project.bean.BuyCartBean;
import com.project.bean.ProductBean;
import com.project.bean.UserBean;
import com.project.service.IBuyCartService;
import com.project.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("buyCart")
@Api(tags="采购车控制器")
public class BuyCartController {
    @Autowired
    private IBuyCartService buyCartService;
    @Autowired
    private IProductService productService;

    @ApiOperation(value = "添加采购车",notes = "将商品添加到采购车中")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name="status",value = "请求状态(add/update)",required = true),
            @ApiImplicitParam(name="num",value = "采购数量"),
    })
    @RequestMapping("add")
    @RequiresPermissions("buy:add")
    public String add(String status, ProductBean product,Integer num, HttpServletRequest req) {
        UserBean loginUser = (UserBean) req.getSession().getAttribute("loginUser");
        BuyCartBean buyCart = new BuyCartBean( loginUser.getId(),product.getId(),num);
        buyCart.setUserId(loginUser.getId());
        if ("add".equals(status)) {
            product.setNum(0);
            productService.add(product);
        }
        buyCartService.add(buyCart);
        return "ok";
    }
    @ApiOperation(value = "查询用户采购车记录")
    @RequestMapping("findByUserId")
    @RequiresPermissions("buy:add")
    public List<BuyCartBean> findByUserId(HttpServletRequest req){
        UserBean loginUser = (UserBean) req.getSession().getAttribute("loginUser");
       return buyCartService.findByUserId(loginUser.getId());
    }
    @ApiOperation(value = "移除采购车商品",notes = "按采购车编号将商品移除采购车")
    @ApiImplicitParam(name="id",value = "采购车编号",required = true)
    @RequestMapping("del")
    @RequiresPermissions("buy:add")
    public String del(Integer id){
        buyCartService.del(id);
        return "ok";
    }
    @ApiOperation(value = "修改采购车商品",notes = "按采购车编号修改采购车的价格、数量")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name="id",value = "采购车编号",required = true),
            @ApiImplicitParam(name="price",value = "采购价格",required = true),
            @ApiImplicitParam(name="num",value = "数量",required = true)
    })
    @RequestMapping("update")
    @RequiresPermissions("buy:add")
    public String update(Integer id, Integer price, Integer num){
        buyCartService.update(id,price,num);
        return "ok";
    }
}
